package com.ehome.poct.base.dao.device.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ehome.core.page.PageInfo;
import com.ehome.mybatisplus.support.AbstractCrudRepository;
import com.ehome.poct.base.api.dto.device.DeviceQry;
import com.ehome.poct.base.api.dto.device.bootModule.TestDeviceBootingHistDTO;
import com.ehome.poct.base.api.vo.device.BootInfoVO;
import com.ehome.poct.base.api.vo.device.TestDeviceBootingHistVO;
import com.ehome.poct.base.dao.device.entity.TestDeviceBootingDetailDO;
import com.ehome.poct.base.dao.device.entity.TestDeviceBootingHistDO;
import com.ehome.poct.base.dao.device.mapper.TestDeviceBootingHistMapper;
import com.ehome.poct.base.dao.mapping.DeviceBootMapping;
import com.ehome.poct.base.device.repository.TestDeviceBootingHistRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 设备开机自检记录RepositoryImpl
 *
 * @author laobai
 * @Date 2022-10-14 17:38
 */
@Repository
public class TestDeviceBootingHistRepositoryImpl extends AbstractCrudRepository<TestDeviceBootingHistDO, TestDeviceBootingHistDTO> implements TestDeviceBootingHistRepository {
    @Autowired
    TestDeviceBootingHistMapper hisMapper;
    @Autowired
    DeviceBootMapping bootMapping;
    /**
     * 获取设备开机记录统计列表信息
     * @param qry 查询条件
     * @return 含分页信息 开机自检概述 几大模块的状态
     */
    @Override
    public PageInfo<TestDeviceBootingHistVO> getSnoDetailList(DeviceQry qry) {
        //page.setTotal(detailMapper.selectCount(Wrappers.<TestDeviceBootingDetailDO>lambdaQuery().eq(TestDeviceBootingDetailDO::getSno,qry.getSno())));
        Page<TestDeviceBootingHistDO> pageInfo  =hisMapper.selectPage(new Page<>(qry.getPageIndex(), qry.getPageSize()), new QueryWrapper<TestDeviceBootingHistDO>()
                .eq(StrUtil.isNotEmpty(qry.getSno()), "sno", qry.getSno()).orderByDesc("booting_check_time")
        );
        PageInfo<TestDeviceBootingHistVO> page = new PageInfo<>();
        page.setList(bootMapping.toList(pageInfo.getRecords()));
        page.setCurrent(qry.getPageIndex());
        page.setSize(qry.getPageSize());
        page.setTotal((int) pageInfo.getTotal());
        return page;
    }
    /**
     * 获取设备开机记录统计列表信息
     * @param qry 查询条件 sno和时间
     * @return 设备的开机自检统计信息 分页
     */
    @Override
    public PageInfo<BootInfoVO> getAllBootInfo(DeviceQry qry){
        PageInfo<BootInfoVO> page = new PageInfo<>();
        page.setCurrent(qry.getPageIndex());
        page.setSize(qry.getPageSize());
        page.setList(hisMapper.getAllBootInfo(qry));
        page.setTotal(hisMapper.getAllCount(qry));
        return page;
    }
}
